package de.fhhof.vinf.ssp.ticketcenter.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ContentServlet
 */
@WebServlet("/ContentServlet")
public class ContentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		String paramSite = request.getParameter("site");
		out.write("<div id=\"outleftcolumnsmall\">");
			out.write("<div id=\"inleftcolumn\">");
				if(paramSite != null && !paramSite.equals("home")){
					this.getContent(request, response, paramSite);
				} else {
					this.printHomepage(request, response, out);
				}
			out.write("</div>");
		out.write("</div>");
	}
	
	/**
	 * Bindet den Inhalt der Startseite ein.
	 * @param request
	 * @param response
	 * @param out
	 */
	private void printHomepage(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		out.println("<table width='100%'><tr><td align='center'><h1>Willkommen beim Ticket-Bestellservice</h1></td></tr></table>");
		out.println("<br/><br/><h2>Letzte Neuigkeiten</h2><br/><hr/>");
		// Letzten 5 News inkludieren 
		try {
			request.getRequestDispatcher("./NewsServlet").include(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 
	 */
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


	private void getContent(HttpServletRequest request, HttpServletResponse response, String paramSite) throws ServletException, IOException {
		if(paramSite == null){
			paramSite = "";
		}		
		RequestDispatcher rq = request.getRequestDispatcher("/"+ paramSite);
		rq.include(request, response);
	}
}
